package com.financing.platform.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ActivitesMollionsWealthDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.ProjectDTO;
import com.financing.platform.service.IActivitiesMollionsWealthService;
import com.financing.platform.service.IFpFundInfoService;
import com.financing.platform.service.IProjectService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;

@Controller("activitiesMollionsWealthActionApi")
@RequestMapping("/api/activities")
@Scope("prototype")
public class ActivitiesMollionsWealthAction extends BaseAction{
	
	@Autowired
	private IActivitiesMollionsWealthService activitiesMollionsWealthServiceImpl;
	
	@Autowired
	private IProjectService projectServiceImpl;
	
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	@RequestMapping("/queryActivitiesMollionsWealth.shtml")
	@ResponseBody
	public ResultMap queryActivitiesMollionsWealth(HttpServletRequest request,
			HttpServletResponse response,ActivitesMollionsWealthDTO dto,Pager pager) {
		try{
			log.info("Begin queryActivitiesMollionsWealth list!");
			
			pager.setTotalCount(activitiesMollionsWealthServiceImpl.queryActivitiesMollionsWealthCount(dto));
			dto.setPageNo(pager.getPageNo());
			dto.setPageSize(pager.getPageSize());
			
			resultMap.setAttribute("page", pager);
			resultMap.setAttribute("list",activitiesMollionsWealthServiceImpl.queryActivitiesMollionsWealthList(dto));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End queryActivitiesMollionsWealth list!");
		}catch(Exception e){
			log.error("queryActivitiesMollionsWealth error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	
	@RequestMapping("/addActivitiesMollionsWealth.shtml")
	@ResponseBody
	public ResultMap addActivitiesMollionsWealth(HttpServletRequest request,
			HttpServletResponse response,ActivitesMollionsWealthDTO dto) {
		try{
			log.info("Begin addActivitiesMollionsWealthProduct list!");
			DTOdateSet.setCreateInfo(dto, request.getSession());
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			
			//判断产品ID是否已经传送进来
			
			//检查参数
			validateMollions(dto);
			
			
			//默认是百万财富
			if(dto.getActivitiesCode()==null){
				dto.setActivitiesCode("BWCFJH");
			}
			resultMap.setAttribute("id",activitiesMollionsWealthServiceImpl.addActivitiesMollionsWealthProduct(dto));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End addActivitiesMollionsWealthProduct list!");
		}catch(Exception e){
			log.error("addActivitiesMollionsWealthProduct error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}

	/**
	 * 
	
	* @Title: validateMollions
	
	* @Description: 校验参数是否合规
	
	* @param @param dto
	* @param @throws Exception    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public void validateMollions(ActivitesMollionsWealthDTO dto) throws Exception {
		Integer pId = dto.getpId();
		String pType=dto.getpType();
		if(StringUtil.isNullOrEmptyStr(String.valueOf(pId))){
			throw new Exception("pid is null");
		}
		if(StringUtil.isNullOrEmptyStr(String.valueOf(pType))){
			throw new Exception("ptype is null");
		}
		
		if(!"01".equals(pType)){
			ProjectDTO projectDto = new ProjectDTO();
			projectDto.setId(pId);
			projectDto = projectServiceImpl.queryProject(projectDto);
			
			//判断产品是否存在
			if(projectDto == null || projectDto.getId() == null){
				throw new Exception("product not exist!");
			}
		}else{
			FpFundInfoDTO fundInfoDto=new FpFundInfoDTO();
			fundInfoDto.setId(pId);
			FpFundInfoDTO fpFundInfo = fpFundInfoService.getFpFundInfo(fundInfoDto);
			//判断产品是否存在
			if(fpFundInfo == null || fpFundInfo.getId() == null){
				throw new Exception("fpFundInfo not exist!");
			}
		}
	}
	
	@RequestMapping("/updateActivitiesMollionsWealth.shtml")
	@ResponseBody
	public ResultMap updateActivitiesMollionsWealth(HttpServletRequest request,
			HttpServletResponse response,ActivitesMollionsWealthDTO dto) {
		try{
			log.info("Begin updateActivitiesMollionsWealthProduct list!");
			DTOdateSet.setCreateInfo(dto, request.getSession());
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			
			//判断产品ID是否已经传送进来
			
			//检查参数
			validateMollions(dto);
			
			resultMap.setAttribute("totalUpdate",activitiesMollionsWealthServiceImpl.updateActivitiesMollionsWealthProduct(dto));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End updateActivitiesMollionsWealthProduct list!");
		}catch(Exception e){
			log.error("updateActivitiesMollionsWealthProduct error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
	@RequestMapping("/deleteActivitiesMollionsWealth.shtml")
	@ResponseBody
	public ResultMap deleteActivitiesMollionsWealth(HttpServletRequest request,
			HttpServletResponse response,ActivitesMollionsWealthDTO dto) {
		try{
			log.info("Begin deleteActivitiesMollionsWealth list!");
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			resultMap.setAttribute("totalUpdate",activitiesMollionsWealthServiceImpl.deleteActivitiesMollionsWealthProduct(dto));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End updateActivitiesMollionsWealthProduct list!");
		}catch(Exception e){
			log.error("updateActivitiesMollionsWealthProduct error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			return resultMap;
	}
}
